package com.gw.quotes.net.service;

import java.util.HashMap;

import org.ksoap2.serialization.SoapObject;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gw.quotes.net.NewException;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;

/**
 * 通用网络请求，返回map数据
 * 
 * @author yxw
 *
 */
public class GetMapInfoImpl extends WebBaseServiceImpl {

	private String method = "";

	public GetMapInfoImpl(String method) {
		this.method = method;
	}

	@Override
	public Response setResponse() {
		return new Response();
	}

	@Override
	public void checkParam(Request request) throws NewException {
		if (request == null) {
			throw new NewException(Response.RESULT_ERROR, "传入request不能为空");
		}
	}

	@Override
	public void setParamByRequest(Request request, SoapObject rpc)
			throws NewException {
		if (request.getParas() != null) {
			for (int i = 0; i < request.getParas().length; i++) {
				rpc.addProperty("arg" + i, request.getParas()[i]);
			}
		}
	}

	@Override
	public String getMethodName() throws NewException {
		return method;
	}

	@Override
	public void resolveResponse(Object object, Response response)
			throws NewException {
		try {
			if (response == null) {
				response = new Response();
			}
			Gson gson = new Gson();
			java.lang.reflect.Type type = new TypeToken<HashMap<String, Object>>() {
			}.getType();
			HashMap<String, Object> map = gson
					.fromJson(object.toString(), type);

			Log.i("从服务端获取的json==:", gson.toJson(map));
			
			if (map != null) {

				response.setResult(Response.RESULT_SUCCESS);
				response.setObj(map);
				setResponse(response);
			} else {
				throw new Exception();
			}
		} catch (Exception e) {
			response.setMsg("获取失败");
			response.setResult(Response.RESULT_ERROR);
		}
	}

}
